<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择信息！','error');
	}
}
$return['sex']=$this->sex;
$return['status']=$this->gong_status;
if($_RQ['op']=='list'){
	$params=array('type'=>0);
	$params['uniacid']=$_SESSION['uniacid'];
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['memberid'])){
		$params['memberid']=intval($_RQ['memberid']);
		$return['params']['memberid']=$_RQ['memberid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aihire_gong',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['memberid']);
			$merch=pdo_get('jy_aihire_merch',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$item['merch']=$merch;
			$item['sex']=pdo_getcolumn('jy_aihire_member',array('id'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']),'sex');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'灵工用工'),
		),
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：这里显示灵工用工情况，包括（实习，在职，离职）</red>','edit'=>true,'list'=>array(
			array('name'=>'member','title'=>'用工信息','type'=>'member'),
			array('name'=>'sex','title'=>'性别','type'=>'radio','options'=>'sex'),
			array('name'=>'merch.title','title'=>'公司名称','type'=>'string'),
			array('name'=>'fee','title'=>'薪资','type'=>'string'),
			array('name'=>'day','title'=>'入职日期','type'=>'string'),
			array('name'=>'jobinfo.title','title'=>'工作名称','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'uptime','title'=>'最近打卡时间','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),'action'=>array(
			//array('title'=>'设置状态','type'=>'modal','op'=>'status'),
		)),
		'search'=>array(
			array('name'=>'mid','width'=>15,'title'=>'用工人','type'=>'choose','action'=>'jy_aihire/get_member/'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['sex']=$this->sex;
	$return['pay']=$this->xinzhis;
	$return['select_status']=array(array('id'=>0,'title'=>'下架'),array('id'=>1,'title'=>'上架'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aihire_job',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['images']=json_decode($item['images'],true);
		$item['tui_ids']=json_decode($item['tui_ids'],true);
		$item['lat_lng']=json_decode($item['lat_lng'],true);
	}else{
		$item=array();
	}
	$item['pcd']=array('province'=>$item['province'],'city'=>$item['city'],'district'=>$item['district']);
	
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'select2','title'=>'求职岗位','name'=>'pid','options'=>'pids','title2'=>'岗位2级分类','name2'=>'cid','options2'=>'cids'),
					array('type'=>'radio','title'=>'性别','name'=>'sex','help'=>'','options'=>'sex'),
					array('type'=>'text','title'=>'求职详情','name'=>'remark','help'=>''),
					array('type'=>'radio','title'=>'薪资','name'=>'pay','help'=>'','options'=>'pay'),
					array('type'=>'pcd','title'=>'地址','name'=>'pcd'),
					array('type'=>'string','title'=>'地址','name'=>'address'),
					array('type'=>'lat_lng','title'=>'坐标','name'=>'lat_lng'),
				    array('name'=>'mid','title'=>'发布人','type'=>'choose','action'=>'core/member.get_member/'),
					array('type'=>'images','title'=>'多图','name'=>'images','help'=>''),
					array('type'=>'string','title'=>'浏览量','name'=>'looks','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
				)
			),
			array(
				'title'=>'关联推送',
					'list'=>array(
						array('type'=>'choose_many','action'=>'jy_aihire/item_list/choose_items/imgtitle=1','name'=>'tui_ids','title'=>'选择要关联的信息'),
					)
			),
		)
	);
	$return['pids']=pdo_getall('jy_aihire_job_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($return['pids'] as &$pv){
		$pv['cids']=pdo_getall('jy_aihire_job_group',array('pid'=>$pv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['pcd']['province']){
			exi('城市不能为空！','error');
	}
	if(!$postdata['images']){
			//exi('图片不能为空！','error');
	}
	if(!$postdata['mid']){
		exi('发布人不能为空！','error');
	}
	if(empty($postdata['lat_lng'])){
			//exi('请选择坐标！','error');
	}
	if($postdata['pcd']){
		if($postdata['pcd']['city']){
			if($postdata['pcd']['city']=='北京市' || $postdata['pcd']['city']=='上海市' || $postdata['pcd']['city']=='天津市'){
				$address=$postdata['pcd']['city'].' 市辖区';
			}else{
				$address=$postdata['pcd']['province'].' '.$postdata['pcd']['city'];
			}
			$code=pdo_getcolumn('core_area',array('address'=>$address),'parent_code');
		}
	}
	$jobname=pdo_getcolumn('jy_aihire_job_group',array('id'=>$item['cid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$company_title=pdo_getcolumn('jy_aihire_merch',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$data=array(
		'title'=>trim($postdata['title'])?:$company_title.'求职['.$jobname.']',
		'images'=>ijson_encode($postdata['images']),
	//	'positionid'=>intval($postdata['positionid']),
		'pay'=>intval($postdata['pay']),
		'mid'=>intval($postdata['mid']),
		'remark'=>trim($postdata['remark']),
		'name'=>trim($postdata['name']),
		'mobile'=>trim($postdata['mobile']),
		'address'=>trim($postdata['address']),
		'status'=>intval($postdata['status']),
		'sex'=>intval($postdata['sex']),
		'province'=>$postdata['pcd']['province'],
		'city'=>$postdata['pcd']['city'],
		'district'=>$postdata['pcd']['district'],
		'code'=>$code,
		'looks'=>intval($postdata['looks']),
		'pid'=>intval($postdata['pid']),
		'cid'=>intval($postdata['cid']),
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
		'paixu'=>intval($postdata['paixu']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aihire_job',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['type']=1;
		pdo_insert('jy_aihire_job',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aihire_job',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aihire_job',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		$basic=$this->plugin_setting('basic');
		$topdays=intval($basic['topdays'])?:7;
		$topendtime=TIMESTAMP+$topdays*24*3600;
		pdo_update('jy_aihire_job',array('ishot'=>intval($post['ishot']),'topendtime'=>$topendtime),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}